﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using VersionComparer.Views;

namespace VersionComparer.DataModel.Actions
{
    [ActionAttribute("Open in notepad",31, System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)]
    public class OpenInNotepadAction : SpecificAction
    {
        public OpenInNotepadAction()
            : base()
        {
        }

        public override bool RefreshAfterFinish
        {
            get
            {
                return false;
            }
            set
            {
            }
        }
        public override bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            if (focusedSelectedItems.Length != 1)
                return false;
            foreach (ComparableItem item in focusedSelectedItems)
            {
                var aux = item;
                while (aux != null)
                {
                    if (!aux.Text.StartsWith("http:", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (File.Exists(aux.Text) )
                            return true;
                    }
                    aux = aux.Parent;
                }
            }
            return false;
        }
        public override System.Drawing.Image Image(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            return Icon.ExtractAssociatedIcon(Environment.SystemDirectory + "\\notepad.exe").ToBitmap();
        }
        protected override void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            foreach (var item in focusedSelectedItems)
            {
                var aux = item;
                while (aux != null)
                {
                    if (File.Exists(aux.Text))
                        break;
                    aux = aux.Parent;
                }
                if (aux != null)
                {
                    
                    Process process = new Process();
                    process.StartInfo = new ProcessStartInfo(@"notepad.exe", "\"" + aux.Text + "\"");
                    process.Start();
                }
            }
        }
    }
}
